#include "ModelRenderPass.h"
#include <SERender/Renderer/Renderer.h>
#include <SEngine/Render/Descriptors/SceneDescriptor.h>
#include <SEngine/Render/Descriptors/ModelDescriptor.h>
#include <SECore/Ecs/Scene.h>
#include <SEngine/Ecs/Components/ModelRenderComp.h>
#include <SEngine/Render/RenderFeature/ModelRenderFeature.h>
#include <SEEdit/Render/RenderFeature/DebugRenderFeature.h>
#include <SEEdit/Descriptors/SelectActorDescriptor.h>

namespace SEngine
{
    ModelRenderPass::ModelRenderPass(Renderer &renderer)
        : RenderPass(renderer) 
    {
    }

    void ModelRenderPass::BeginFrame()
    {
        auto sceneDes = m_renderer.AnyDescriptor::GetDescriptor<SceneDescriptor>();
        if (!sceneDes || !sceneDes->get().scene) {
            return;
        }
        auto scene = sceneDes->get().scene;
        ModelDescriptor modelDes;
        scene->Components<ModelRenderComp>([&](Actor & actor, ModelRenderComp & comp) {
            modelDes.models.emplace_back(&comp.m_model);
        });
        m_renderer.AnyDescriptor::AddDescriptor<ModelDescriptor>(std::move(modelDes));
    }

    void ModelRenderPass::DrawFrame()
    {
        auto debugFeature = m_renderer.Descriptor<RenderFeature>::GetDescriptor<DebugRenderFeature>();
        if (!debugFeature) {
            return;
        }
        auto modelRenderFeature = m_renderer.Descriptor<RenderFeature>::GetDescriptor<ModelRenderFeature>();
        if (!modelRenderFeature) {
            return;
        }
        auto modelDes = m_renderer.AnyDescriptor::GetDescriptor<ModelDescriptor>();
        if (!modelDes) {
            return;
        }
        for (auto & model : modelDes->get().models) {
            modelRenderFeature->get().DrawModel(*model);
        }
    }

} // namespace SEngine

